﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using MigratorCore.Actions;
using MigratorCore.Bases;

namespace MigratorWizard
{
    public partial class ActionList : UserControl
    {
        private Main main;

        public Main Main
        {
            set { main = value; main.NextClick += new NextClick(main_NextClick); }
        }

        void main_NextClick(object sender, EventArgs e)
        {
            if (this.Visible)
            {
                main.Migrator.DestinationActions.Clear();
                main.Migrator.SourceActions.Clear();
                foreach (ListViewItem i in listView2.Items)
                    main.Migrator.SourceActions.Add((ISourceAction)main.Migrator.Source.getAction(i.Text));
                foreach (ListViewItem i in listView3.Items)
                    main.Migrator.DestinationActions.Add((IDestinationAction)main.Migrator.Destination.getAction(i.Text));
            }
        }

        public ActionList()
        {
            InitializeComponent();
        }
        private bool lv1_mdown = false;
        private bool lv2_mdown = false;
        private bool lv3_mdown = false;
        private actionm movetype = actionm.NULL;

        private void listView1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left && e.Clicks == 1)
                lv1_mdown = true;
        }

        private void listView2_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left && e.Clicks == 1)
                lv2_mdown = true;
        }

        private void listView3_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left && e.Clicks == 1)
                lv3_mdown = true;
        }

        private void listView1_MouseMove(object sender, MouseEventArgs e)
        {
            if (!lv1_mdown) return;
            if (e.Button == MouseButtons.Right) return;
            string str = GetItemText(listView1);
            if (str == "") return;
            movetype = GetItemType(listView1);
            listView1.DoDragDrop(str, DragDropEffects.Move);
        }

        private void listView2_MouseMove(object sender, MouseEventArgs e)
        {
            if (!lv2_mdown) return;
            if (e.Button == MouseButtons.Right) return;
            string str = GetItemText(listView2);
            if (str == "") return;
            movetype = GetItemType(listView2);
            listView2.DoDragDrop(str, DragDropEffects.Move);
        }

        private void listView3_MouseMove(object sender, MouseEventArgs e)
        {
            if (!lv3_mdown) return;
            if (e.Button == MouseButtons.Right) return;
            string str = GetItemText(listView3);
            if (str == "") return;
            movetype = GetItemType(listView3);
            listView3.DoDragDrop(str, DragDropEffects.Move);
        }

        private void listView1_DragDrop(object sender, DragEventArgs e)
        {
            if (movetype == actionm.NULL) return;
            string textBox1 = e.Data.GetData(DataFormats.Text).ToString();
            string[] items = textBox1.Split(',');
            ListViewItem i = new ListViewItem(items);
            i.Group = listView1.Groups[((movetype == actionm.Source) ? 0 : 1)];
            removeFromLV1(i);
            removeFromLV2(i);
            removeFromLV3(i);
            listView1.Items.Add(i);
            lv1_mdown = false;
            lv2_mdown = false;
            lv3_mdown = false;
            movetype = actionm.NULL;
            Check();
        }

        private void listView2_DragDrop(object sender, DragEventArgs e)
        {
            if (movetype == actionm.NULL) return;
            string textBox1 = e.Data.GetData(DataFormats.Text).ToString();
            string[] items = textBox1.Split(',');
            removeFromLV1(new ListViewItem(items, 0));
            removeFromLV2(new ListViewItem(items, 0));
            listView2.Items.Add(new ListViewItem(items, 0));
            lv1_mdown = false;
            lv2_mdown = false;
            lv3_mdown = false;
            movetype = actionm.NULL;
            Check();
        }

        private void listView3_DragDrop(object sender, DragEventArgs e)
        {
            if (movetype == actionm.NULL) return;
            string textBox1 = e.Data.GetData(DataFormats.Text).ToString();
            string[] items = textBox1.Split(',');
            removeFromLV1(new ListViewItem(items, 0));
            removeFromLV3(new ListViewItem(items, 0));
            listView3.Items.Add(new ListViewItem(items, 0));
            lv1_mdown = false;
            lv2_mdown = false;
            lv3_mdown = false;
            movetype = actionm.NULL;
            Check();
        }

        private void removeFromLV1(ListViewItem item)
        {
            foreach (ListViewItem i in listView1.Items)
                if (i.Text == item.Text && i.Group == listView1.Groups[((movetype == actionm.Source) ? 0 : 1)])
                {
                    listView1.Items.Remove(i);
                    return;
                }
        }

        private void removeFromLV2(ListViewItem item)
        {
            if (movetype != actionm.Source) return;
            foreach (ListViewItem i in listView2.Items)
                if (i.Text == item.Text)
                {
                    listView2.Items.Remove(i);
                    return;
                }
        }

        private void removeFromLV3(ListViewItem item)
        {
            if (movetype != actionm.Destination) return;
            foreach (ListViewItem i in listView3.Items)
                if (i.Text == item.Text)
                {
                    listView3.Items.Remove(i);
                    return;
                }
        }

        private void listView1_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.Text))
                e.Effect = DragDropEffects.Move;
            else
                e.Effect = DragDropEffects.None;
        }

        private void listView2_DragEnter(object sender, DragEventArgs e)
        {
            if (movetype == actionm.NULL | movetype == actionm.Destination)
                e.Effect = DragDropEffects.None;
            else
            {
                if (e.Data.GetDataPresent(DataFormats.Text))
                    e.Effect = DragDropEffects.Move;
                else
                    e.Effect = DragDropEffects.None;
            }
        }

        private void listView3_DragEnter(object sender, DragEventArgs e)
        {
            if (movetype == actionm.NULL | movetype == actionm.Source)
                e.Effect = DragDropEffects.None;
            else
            {
                if (e.Data.GetDataPresent(DataFormats.Text))
                    e.Effect = DragDropEffects.Move;
                else
                    e.Effect = DragDropEffects.None;
            }
        }

        private string GetItemText(ListView LVIEW)
        {
            int nTotalSelected = LVIEW.SelectedIndices.Count;
            if (nTotalSelected <= 0) return "";
            IEnumerator selCol = LVIEW.SelectedItems.GetEnumerator();
            selCol.MoveNext();
            ListViewItem lvi = (ListViewItem)selCol.Current;
            string mDir = "";
            for (int i = 0; i < lvi.SubItems.Count; i++)
                mDir += lvi.SubItems[i].Text + ",";
            mDir = mDir.Substring(0, mDir.Length - 1);
            return mDir;
        }

        private enum actionm { Source, Destination, NULL };

        private actionm GetItemType(ListView LVIEW)
        {
            int nTotalSelected = LVIEW.SelectedIndices.Count;
            if (nTotalSelected < 1) return actionm.NULL;
            ListViewItem lvi = LVIEW.SelectedItems[0];
            if (LVIEW.Name == listView1.Name)
            {
                if (lvi.Group == listView1.Groups[0])
                    return actionm.Source;
                else return actionm.Destination;
            }
            else if (LVIEW.Name == listView2.Name)
                return actionm.Source;
            else if (LVIEW.Name == listView3.Name)
                return actionm.Destination;
            return actionm.NULL;
        }

        private void ActionList_VisibleChanged(object sender, EventArgs e)
        {
            if (this.Visible && (main != null))
            {
                this.main.EnableNext = false;
                this.main.EnableBack = true;
                this.main.Text = "The Migrator Project - Actions Order";
                listView1.Items.Clear();
                listView2.Items.Clear();
                listView3.Items.Clear();
                foreach (IAction action in main.Migrator.Source.Actions)
                    if (action.GetType().GetInterfaces()[0] == typeof(ISourceAction))
                        listView1.Items.Add(new System.Windows.Forms.ListViewItem(new string[] {
                            action.Name,
                            action.ActionType.ToString()}, listView1.Groups[0]));
                foreach (IAction action in main.Migrator.Destination.Actions)
                    if (action.GetType().GetInterfaces()[0] == typeof(IDestinationAction))
                        listView1.Items.Add(new System.Windows.Forms.ListViewItem(new string[] {
                            action.Name,
                            action.ActionType.ToString()}, listView1.Groups[1]));
            }
        }

        private void btnUp_Click(object sender, EventArgs e)
        {
            if (listView2.SelectedItems.Count > 0)
            {
                int sel = listView2.SelectedIndices[0];
                if (sel > 0)
                {
                    ListViewItem i = listView2.Items[sel - 1];
                    listView2.Items.Remove(i);
                    listView2.Items.Insert(sel, i);
                    listView2.SelectedItems.Clear();
                    listView2.SelectedIndices.Clear();
                    listView2.SelectedIndices.Add(sel - 1);
                }
            }
            if (listView3.SelectedItems.Count > 0)
            {
                int sel = listView3.SelectedIndices[0];
                if (sel > 0)
                {
                    ListViewItem i = listView3.Items[sel - 1];
                    listView3.Items.Remove(i);
                    listView3.Items.Insert(sel, i);
                    listView3.SelectedItems.Clear();
                    listView3.SelectedIndices.Clear();
                    listView3.SelectedIndices.Add(sel - 1);
                }
            }
        }

        private void btnDown_Click(object sender, EventArgs e)
        {
            if (listView2.SelectedItems.Count > 0)
            {
                int sel = listView2.SelectedIndices[0];
                if (sel < listView2.Items.Count - 1)
                {
                    ListViewItem i = listView2.SelectedItems[0];
                    listView2.Items.Remove(i);
                    listView2.Items.Insert(sel + 1, i);
                    listView2.SelectedIndices.Clear();
                    listView2.SelectedItems.Clear();
                    listView2.SelectedIndices.Add(sel + 1);
                }
            }
            if (listView3.SelectedItems.Count > 0)
            {
                int sel = listView3.SelectedIndices[0];
                if (sel < listView3.Items.Count - 1)
                {
                    ListViewItem i = listView3.SelectedItems[0];
                    listView3.Items.Remove(i);
                    listView3.Items.Insert(sel + 1, i);
                    listView3.SelectedIndices.Clear();
                    listView3.SelectedItems.Clear();
                    listView3.SelectedIndices.Add(sel + 1);
                }
            }
        }

        private void listView_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnDown.Enabled = false;
            btnUp.Enabled = false;
            if (listView2.SelectedItems.Count > 0)
            {
                if (listView2.SelectedIndices[0] > 0) btnUp.Enabled = true;
                if (listView2.SelectedIndices[0] < listView2.Items.Count - 1) btnDown.Enabled = true;
            }
            if (listView3.SelectedItems.Count > 0)
            {
                if (listView3.SelectedIndices[0] > 0) btnUp.Enabled = true;
                if (listView3.SelectedIndices[0] < listView3.Items.Count - 1) btnDown.Enabled = true;
            }
        }

        private void listView1_MouseUp(object sender, MouseEventArgs e)
        {
            lv1_mdown = false;
        }

        private void listView2_MouseUp(object sender, MouseEventArgs e)
        {
            lv2_mdown = false;
        }

        private void listView3_MouseUp(object sender, MouseEventArgs e)
        {
            lv3_mdown = false;
        }

        private void listView2_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
                if (listView2.SelectedIndices.Count > 0)
                {
                    SettingsDialog sd = new SettingsDialog(main.Migrator.Source.getAction(listView2.SelectedItems[0].Text).Settings, main.Migrator.Source.getAction(listView2.SelectedItems[0].Text).Name);
                    sd.ShowDialog(this);
                }
        }

        private void listView3_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
                if (listView3.SelectedIndices.Count > 0)
                {
                    SettingsDialog sd = new SettingsDialog(main.Migrator.Destination.getAction(listView3.SelectedItems[0].Text).Settings, main.Migrator.Destination.getAction(listView3.SelectedItems[0].Text).Name);
                    sd.ShowDialog(this);
                }
        }

        private void Check()
        {
            if (listView2.Items.Count > 0 && listView3.Items.Count == listView2.Items.Count) main.EnableNext = true;
            else main.EnableNext = false;
        }
    }
}
